// router.js
import { checkPermission, getRedirectPath } from "./permission";
import { ROUTE_PATHS } from "./routes";
import { RouterMount, createRouter } from "uni-simple-router";

const router = createRouter({
  platform: process.env.VUE_APP_PLATFORM,
  routes: [
    {
      path: ROUTE_PATHS.INDEX,
      redirect: ROUTE_PATHS.HOME,
    },
    {
      path: ROUTE_PATHS.LOGIN,
      Component: () => import("@/pages/login/index"),
    },
    {
      path: ROUTE_PATHS.HOME,
      Component: () => import("@/pages/home/index"),
    },
    {
      path: ROUTE_PATHS.SEARCH,
      Component: () => import("@/pages/search/index"),
    },
    {
      path: ROUTE_PATHS.APPOINTMENT,
      Component: () => import("@/pages/appointment/index"),
    },
    {
      path: ROUTE_PATHS.APPOINTMENT_DETAIL,
      Component: () => import("@/pages/appointment-detail/index"),
    },
    {
      path: ROUTE_PATHS.APPOINTMENT_LIST,
      Component: () => import("@/pages/appointment-list/index"),
    },
    {
      path: ROUTE_PATHS.USER_CENTER,
      Component: () => import("@/pages/user-center/index"),
    },
    {
      path: ROUTE_PATHS.CONFIRM,
      Component: () => import("@/pages/confirm/index"),
    },
    {
      path: ROUTE_PATHS.AUDIT,
      Component: () => import("@/pages/audit/index"),
    },
    // 添加更多路由
  ],
});

//全局路由前置守卫
router.beforeEach((to, from, next) => {
  if (checkPermission(to.path)) {
    next();
  } else {
    const redirectPath = getRedirectPath(to.path);
    next({ path: redirectPath, query: { redirect: to.fullPath } });
  }
});
// 全局路由后置守卫
router.afterEach((to, from) => {});

export { RouterMount, router };
